package com.rover022.flixelgame.component.ai
{
	import com.rover022.flixelgame.element.BaseElement;
	import com.rover022.flixelgame.element.PLAYSTATE;
	import com.rover022.flixelgame.manager.PlayerManager;

	/**
	 * 攻击行为
	 * 横向行走
	 * 找到对手少于自己的最小攻击距离后停下来 播放攻击动作
	 * @author flashpig
	 *
	 */
	public class OrdinaryFighterAI extends AiComponet
	{
		public var parent:BaseElement;
		/**
		 * 寻路路径数组
		 */
		public var roadPath:Array;
		/**
		 * 计算方式 是只计算X 还是只计算Y 还是计算Vector距离
		 * 0 计算Vector距离
		 * 1 计算x距离
		 * 2 计算y距离
		 */
		public var calculationMode:uint = 1;

		public function OrdinaryFighterAI(parentRef:BaseElement, movementType:int=1, stoppingType:int=0, updateFacing:Boolean = false, enableArrowKeys:Boolean = true)
		{
			parent = parentRef;
			super(parentRef, movementType, stoppingType, updateFacing, enableArrowKeys);
		}

		override public function update():void
		{
			if(parent.isBusy){
				return;
			}
			var inArea:Boolean = false; //是否在攻击范围;
			var _players:Array = PlayerManager.getInstance().getOpponentsExcept(parent.factionType);
			var _pponent:BaseElement = PlayerManager.getInstance().InAttackArea(parent, _players, calculationMode);
			if (_pponent)
			{
				//如果在攻击范围内
				if(parent.state == PLAYSTATE.Stand||parent.state==PLAYSTATE.Walk){
					parent.sightTo(_pponent);
				}
			}
			else
			{
				//否则停下来..
				if (_players.length > 0)
				{
					PlayerManager.getInstance().moveTo(parent, _players[0], calculationMode);
				}
				else
				{
					parent.bodyElement.velocity.x = 0;
					parent.bodyElement.velocity.y = 0;
				}
			}
			super.update();
		}
		
		
		
		
	}
}
